#include<string>
#include<vector>
#include<array>
#include<unordered_map>
#include<algorithm>
#include<limits>
#include <stack>
#include<iostream>
#include<sstream>
#include<deque>
#define MOD 1000000007
struct node
{
    int first;
    int second;
};
class Solution {
public:
    int findMaxValueOfEquation(std::vector<std::vector<int>>& points, int k) {
        int  n = points.size();
        int ans = std::numeric_limits<int>::min();
        std::deque<node> q;
        for(int i=0;i<n;i++)
        {
            int x = points[i][0];
            int y = points[i][1];
            while (!q.empty()&&q.front().first +k<x)
            {
                q.pop_front();
            }
            if (!q.empty())
            {
                ans = std::max(ans, x+y+q.front().second-q.front().first);
            }
            while (!q.empty()&& q.back().second-q.back().first<=y-x)
            {
                q.pop_back();
            }
            q.push_back({x,y});
        }
        return ans;
    }
};